import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/entities/format/split_expense_entity.dart';
import 'package:finance_app/src/entities/setting_data_entity.dart';
import 'package:finance_app/src/router_gen/router_gen_providers.dart';

mixin FormCategoryGstMixin<T> on BaseProvider<T> {
  // select category
  List<SplitExpenseEntity> splitList = [];
  SettingDataCurrency unit;
  double get subTotal {
    double total = 0.0;
    splitList.forEach((element) {
      total += double.parse(element.amount, (e) => 0.0);
    });
    return total;
  }

  double get total {
    return subTotal + gst;
  }

  double get gst {
    double total = 0.0;
    splitList.forEach((element) {
      if (element.isCheckInclusive == false) {
        total += double.parse(element.amount, (e) => 0.0) *
            (element.selectTax?.rate ?? 0.0) /
            100;
      }
    });
    return total;
  }

  double get includesGST {
    double total = 0.0;
    splitList.forEach((element) {
      if (element.isCheckInclusive) {
        total += calInclusiveGST(
            double.parse(element.amount), (element.selectTax?.rate ?? 0) / 100);
      }
    });
    return total;
  }

  double calInclusiveGST(num amount, num gstRate) {
    if (amount == 0) return 0.0;
    return amount - (amount / (1 + gstRate ?? 0)).toDouble();
  }

  void unFocus();

  Future<void> onSelectItem(SplitExpenseEntity value, String type) async {
    unFocus();
    final result = await Routes.navigateTo(
        RouterGenProviders.amountGstRateCategoryPage,
        params: {
          'category': value.selectCategory,
          'amount': value.amount,
          'tax': value.selectTax,
          'unit': unit,
          'isCheckInclusive': value.isCheckInclusive,
          'type': type
        });
    if (result != null) {
      bool delete = result['delete'];
      if (delete == true) {
        splitList.remove(value);
      } else {
        value.selectCategory = result['category'];
        value.amount = result['amount'];
        value.selectTax = Config.currentCompany.enableGst == 1
            ? result['gst']
            : Config.noneTax;
        value.isCheckInclusive = result['isCheckInclusive'];
        // value.number = result['data'].number;
      }
      update();
    }
  }
}
